﻿using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace LuckyFish.Blog.Migrations
{
    /// <inheritdoc />
    public partial class AddListAndLable : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Labels",
                table: "Articles");

            migrationBuilder.DropColumn(
                name: "List",
                table: "Articles");

            migrationBuilder.AddColumn<string>(
                name: "ListId",
                table: "Articles",
                type: "varchar(256)",
                nullable: true);

            migrationBuilder.CreateTable(
                name: "ArticleLists",
                columns: table => new
                {
                    ListName = table.Column<string>(type: "varchar(256)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ArticleLists", x => x.ListName);
                });

            migrationBuilder.CreateTable(
                name: "LabelLists",
                columns: table => new
                {
                    LabelName = table.Column<string>(type: "varchar(256)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_LabelLists", x => x.LabelName);
                });

            migrationBuilder.CreateTable(
                name: "ArticleModelLabelList",
                columns: table => new
                {
                    ArticlesUrlName = table.Column<string>(type: "varchar(256)", nullable: false),
                    LabelsLabelName = table.Column<string>(type: "varchar(256)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ArticleModelLabelList", x => new { x.ArticlesUrlName, x.LabelsLabelName });
                    table.ForeignKey(
                        name: "FK_ArticleModelLabelList_Articles_ArticlesUrlName",
                        column: x => x.ArticlesUrlName,
                        principalTable: "Articles",
                        principalColumn: "UrlName",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ArticleModelLabelList_LabelLists_LabelsLabelName",
                        column: x => x.LabelsLabelName,
                        principalTable: "LabelLists",
                        principalColumn: "LabelName",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Articles_ListId",
                table: "Articles",
                column: "ListId");

            migrationBuilder.CreateIndex(
                name: "IX_ArticleModelLabelList_LabelsLabelName",
                table: "ArticleModelLabelList",
                column: "LabelsLabelName");

            migrationBuilder.AddForeignKey(
                name: "FK_Articles_ArticleLists_ListId",
                table: "Articles",
                column: "ListId",
                principalTable: "ArticleLists",
                principalColumn: "ListName");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Articles_ArticleLists_ListId",
                table: "Articles");

            migrationBuilder.DropTable(
                name: "ArticleLists");

            migrationBuilder.DropTable(
                name: "ArticleModelLabelList");

            migrationBuilder.DropTable(
                name: "LabelLists");

            migrationBuilder.DropIndex(
                name: "IX_Articles_ListId",
                table: "Articles");

            migrationBuilder.DropColumn(
                name: "ListId",
                table: "Articles");

            migrationBuilder.AddColumn<string>(
                name: "Labels",
                table: "Articles",
                type: "TEXT",
                nullable: false,
                defaultValue: "");

            migrationBuilder.AddColumn<string>(
                name: "List",
                table: "Articles",
                type: "TEXT",
                nullable: true);
        }
    }
}
